package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;

import java.util.ArrayList;
import java.util.List;

public class SelectMoneyAdp extends RecyclerView.Adapter<SelectMoneyAdp.SelectMoneyHolder>{

    private List<String> mData;

    private List<String> mSelectData;

    private LayoutInflater inflater;

    private Context mContext;

    public SelectMoneyAdp(Context mContext){
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.mData = new ArrayList<>();
        this.mSelectData = new ArrayList<>();
    }

    public void setData(List<String> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public SelectMoneyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_select_money, parent,false);
        return new SelectMoneyHolder(view);
    }

    @Override
    public void onBindViewHolder(SelectMoneyHolder holder, final int position) {
        holder.update(mData.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check(mData.get(position));
                if(listener != null){
                    listener.onItemClick(mData.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class SelectMoneyHolder extends RecyclerView.ViewHolder{

        private TextView mMoneyTv;

        public SelectMoneyHolder(View itemView) {
            super(itemView);
            mMoneyTv = itemView.findViewById(R.id.item_money_tv);
        }

        public void update(String money){
            mMoneyTv.setText(money);
            if(isSelect(money)){
                mMoneyTv.setBackgroundResource(R.drawable.shape_money_select);
                mMoneyTv.setTextColor(mContext.getResources().getColor(R.color.base_white));
            }else{
                mMoneyTv.setBackgroundResource(R.drawable.shape_money_unselect);
                mMoneyTv.setTextColor(mContext.getResources().getColor(R.color.text_normal_black));
            }
        }
    }

    private void check(String money){
        mSelectData.clear();
        mSelectData.add(money);
        notifyDataSetChanged();
    }

    private boolean isSelect(String money){
        return mSelectData.contains(money);
    }

    public List<String> getSelectData(){
        return mSelectData;
    }

    private SelectMoneyListener listener;

    public void setSelectMoneyListener(SelectMoneyListener listener){
        this.listener = listener;
    }

    public interface SelectMoneyListener{
        void onItemClick(String money);
    }
}
